import { useContext } from "react";
import { GlobalModel } from "@/ui/context";
import { XHSMention } from "@common/models";
import { getNote } from "@/ui/utils/rpc/xhs";
import { getValueOrDie } from "@common/utils";
import { message } from "antd";

export function useViewModel() {
  const globalModel = useContext(GlobalModel);

  const onMentionClick = (mention: XHSMention) => async () => {
    if (!globalModel?.sessions.length) {
      message.error("请先登录账号");
      return;
    }

    const model = getValueOrDie(globalModel);
    if (mention.item_info.id === model.discussion?.note.noteId) {
      return;
    }
    delete model.discussion;

    const note = await getNote({
      noteId: mention.item_info.id,
      xsecToken: mention.item_info.xsec_token,
    });
    model.discussion = {
      note,
      ownerId: mention.currentUserId,
      comments: {
        hasMore: true,
        records: [],
      },
      sendQueue: [],
    };
  };

  return { globalModel, onMentionClick };
}
